using System.Data;
using System;
using Library.Business;
using NhatThanhSoftware;
namespace Library.Controller
{
    public partial class Controller_KhachHang: Cls_KhachHang
    {
        /// <summary>
        /// Set du lieu
        /// </summary>
        private static void setKhachHang(ref Cls_KhachHang KhachHang, DataRow dr)
        {
            KhachHang.MSKH = dr[Cls_KhachHang.fn_MSKH].ToString() ;
            KhachHang.ID_HeThongNuoc = dr[Cls_KhachHang.fn_ID_HeThongNuoc].ToString() ;
            KhachHang.ID_LoaiHinhDoanhNghiep = dr[Cls_KhachHang.fn_ID_LoaiHinhDoanhNghiep].ToString() ;
            KhachHang.TenKhachHang = dr[Cls_KhachHang.fn_TenKhachHang].ToString() ;
            KhachHang.TenKhachHangTat = dr[Cls_KhachHang.fn_TenKhachHangTat].ToString() ;
            KhachHang.DiaChi = dr[Cls_KhachHang.fn_DiaChi].ToString() ;
            KhachHang.MaSoThue = dr[Cls_KhachHang.fn_MaSoThue].ToString() ;
            KhachHang.DienThoai = dr[Cls_KhachHang.fn_DienThoai].ToString() ;
            KhachHang.MucChiTeu = Convert.ToInt32(dr[Cls_KhachHang.fn_MucChiTeu] ) ;
            KhachHang.Hidden = Convert.ToBoolean(dr[Cls_KhachHang.fn_Hidden] ) ;
        }
        /// <summary>
        /// Lưu dữ liệu
        /// </summary>
        /// <param name="dtData">Datatable</param>
        /// <returns>TRUE;FALSE</returns>
        public static bool doSAVE(DataTable dtData)
        {
            if (dtData.GetChanges() == null) return false;
            foreach( DataRow dr in dtData.GetChanges().Rows )
            {
                Cls_KhachHang KhachHang = new Cls_KhachHang();
                try
                {
                    switch(dr.RowState)
                    {
                        case DataRowState.Modified:
                            setKhachHang(ref KhachHang, dr);
                            KhachHang.ID_KhachHang_find =Convert.ToInt32(dr[Cls_KhachHang.fn_ID_KhachHang] );
                            KhachHang.ID_KhachHang=KhachHang.ID_KhachHang_find;
                            KhachHang.doUpdate();
                            break;
                        case DataRowState.Added:
                            setKhachHang(ref KhachHang, dr);
                            KhachHang.doInsert();
                            break;
                        case DataRowState.Deleted:
                            KhachHang.ID_KhachHang_find =Convert.ToInt32(dr[Cls_KhachHang.fn_ID_KhachHang, DataRowVersion.Original] );
                            KhachHang.doDelete();
                            break;
                    }
                }
                catch (Exception ex)
                {
                    ThongBao.msgThongBao(KhachHang.ID_KhachHang + " chưa lưu được, click nút [OK] để lưu tiếp.");
                }
        }
        return true;
        }
        /// <summary>
        /// Check dữ liệu
        /// </summary>
        /// <param name="dtData">Datatable</param>
        /// <returns>TRUE;FALSE</returns>}
        public static void doCHECK(DataTable dtData)
        {
            if (dtData.GetChanges() == null) return;
            DataRowCollection drColection = dtData.GetChanges().Rows;
            foreach (DataRow dr in drColection)
            {
                if (dr.RowState == DataRowState.Deleted) break;
                string strID_KhachHang = dr[Cls_KhachHang.fn_ID_KhachHang].ToString();
                if (dr.RowState == DataRowState.Added)
                {
                }
            }
        }
    }
}
